package com.wyzz.hr.action;

import java.util.List;
import org.apache.log4j.Logger;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.bo.RoleBo;
import com.wyzz.hr.bo.UserBo;
import com.wyzz.hr.pojo.Role;

public class RoleAction extends BaseAction {

	public RoleAction() {
	}

	public String execute() throws Exception {
		if (authId == null) {
			addErrorInfo("\u6B64\u89D2\u8272\u672A\u8BBE\u7F6E\u6743\u9650\uFF01");
			return "input";
		}
		if (authId != null && authId.length > 85) {
			addErrorInfo("\u6B64\u89D2\u8272\u6240\u5173\u8054\u7684\u6743\u9650\u8FC7\u591A\uFF01");
			return "input";
		}
		if (getRoleBo().findRoleByName(role.getRoleName())) {
			addActionError("\u8BE5\u89D2\u8272\u540D\u5DF2\u5B58\u5728!");
			return "input";
		}
		String msg = getRoleBo().addRole(role, authId);
		if ("yes".equals(msg)) {
			addSuccessInfo("\u6DFB\u52A0\u89D2\u8272\u6210\u529F\u3002");
			return "success";
		}
		if ("noAuth".equals(msg)) {
			addErrorInfo("\u6B64\u89D2\u8272\u672A\u8BBE\u7F6E\u6743\u9650\uFF01");
			return "input";
		}
		if ("authOver".equals(msg)) {
			addErrorInfo("\u6B64\u89D2\u8272\u6240\u5173\u8054\u7684\u6743\u9650\u8FC7\u591A\uFF01");
			return "input";
		}
		if ("roleOver".equals(msg)) {
			addErrorInfo("\u60A8\u6240\u521B\u5EFA\u7684\u89D2\u8272\u6570\u91CF\u8FC7\u591A\uFF01");
			return "input";
		}
		if ("no".equals(msg)) {
			addErrorInfo("\u521B\u5EFA\u89D2\u8272\u51FA\u73B0\u5F02\u5E38\uFF01");
			return "input";
		} else {
			return "success";
		}
	}

	public String update() throws Exception {
		if (role == null || authId == null) {
			clearErrorsAndMessages();
			return "success";
		}
		String msg = getRoleBo().updateRole(role, authId, getUserBo());
		if (msg.equals("yes")) {
			addSuccessInfo("\u89D2\u8272\u4FEE\u6539\u6210\u529F\u3002");
			return "success";
		}
		if ("noAuth".equals(msg)) {
			addErrorInfo("\u6B64\u89D2\u8272\u672A\u8BBE\u7F6E\u6743\u9650\uFF01");
			return "input";
		}
		if ("nullAuth".equals(msg)) {
			addErrorInfo("\u65E0\u6CD5\u83B7\u53D6\u7528\u6237\u6743\u9650\uFF0C\u65E0\u6CD5\u66F4\u65B0\uFF01");
			return "input";
		}
		if ("authOver".equals(msg)) {
			addErrorInfo("\u6B64\u89D2\u8272\u6240\u5173\u8054\u7684\u6743\u9650\u8FC7\u591A\uFF01");
			return "input";
		}
		if ("no".equals(msg)) {
			addErrorInfo("\u521B\u5EFA\u89D2\u8272\u51FA\u73B0\u5F02\u5E38\uFF01");
			return "input";
		}
		if ("exist".equals(msg)) {
			addErrorInfo("\u8BE5\u89D2\u8272\u540D\u5DF2\u7ECF\u5B58\u5728\uFF0C\u8BF7\u91CD\u65B0\u8F93\u5165\u4E00\u4E2A\u89D2\u8272\u540D\uFF01");
			return "input";
		} else {
			return "success";
		}
	}

	public RoleBo getRoleBo() {
		if (roleBo == null)
			roleBo = (RoleBo) getBean("roleService");
		return roleBo;
	}

	public UserBo getUserBo() {
		if (userBo == null)
			userBo = (UserBo) getBean("userService");
		return userBo;
	}

	public List getAuthList() {
		return authList;
	}

	public void setAuthList(List authList) {
		this.authList = authList;
	}

	public Integer[] getAuthId() {
		return authId;
	}

	public void setAuthId(Integer authId[]) {
		this.authId = authId;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	private static final long serialVersionUID = 1L;
	static Logger logger = Logger.getLogger(RoleAction.class);
	private UserBo userBo;
	private RoleBo roleBo;
	private List authList;
	private Role role;
	private Integer authId[];

}
